package com.example.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.result.Result;
import com.example.entity.mes.*;
import com.example.mapper.RepairMapper;
import com.example.service.RepairService;
import com.example.untils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 14501
* @description 针对表【repair】的数据库操作Service实现
* @createDate 2024-03-15 10:43:22
*/
@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {

    @Resource
    private RepairMapper repairMapper;

    @Override
    public Result queryRepairIPageList(Integer current,Integer size,Repair repair) {
        try{
            IPage<Repair> page;
            IPage<Repair> config = new Page<>(current,size);
            // 解决当搜索条件为空时，默认查询所有数据
            QueryWrapper<Repair> maintainQueryWrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(repair.getCode())){
                maintainQueryWrapper.like("code",repair.getCode());
            }
            if (!StringUtils.isEmpty(repair.getName())){
                maintainQueryWrapper.like("name",repair.getName());
            }
            if (!StringUtils.isEmpty(repair.getCoding())){
                maintainQueryWrapper.like("coding",repair.getCoding());
            }
            if (!StringUtils.isEmpty(repair.getEq_name())){
                maintainQueryWrapper.like("eq_name",repair.getEq_name());
            }
            if (!StringUtils.isEmpty(repair.getResult())){
                maintainQueryWrapper.like("result",repair.getResult());
            }
            if (!StringUtils.isEmpty(repair.getStatus())){
                maintainQueryWrapper.like("status",repair.getStatus());
            }
            page = this.repairMapper.selectPage(config, maintainQueryWrapper);
            // 每页展示的数据
            List<Repair> repairList = page.getRecords();
            // 将响应数据填充到map中
            if (page.getTotal()==0){
                return Result.error("暂无数据");
            }
            Map map = new HashMap();
            map.put("pages", page.getPages());
            map.put("total", page.getTotal());
            map.put("list", repairList);
            return Result.success(map);
        }catch (Exception e) {
            return Result.error("分页查询异常" + String.valueOf(e),"500");
        }
    }

    @Override
    public Result queryRepairById(Long id) {
        try{
            Repair repair = this.repairMapper.selectById(id);
            if (repair==null){
                return Result.error("未查询到数据","500");
            }
            return Result.success(repair);
        }catch (Exception e){
            return Result.error("通过id进行查询发生异常"+String.valueOf(e),"500");
        }
    }

    @Override
    public Result updateRepair(Repair repair) {
        try {
            int insert = this.repairMapper.updateById(repair);
            if (insert>0){
                return Result.success();
            }else {
                return Result.error("修改失败","500");
            }
        }catch (Exception e){
            return Result.error("修改发生异常"+String.valueOf(e),"500");
        }
    }

    @Override
    @DS("mes")
    @Transactional(rollbackFor = Exception.class) // 批量执行需要加上事务,其中报错,进行回滚操作
    public Result deleteRepairBatch(List<Integer> ids) {
        try {
            // 在多数据源下 此方法需要再Service实现类中的方法上面加上数据源区别注解(@DS（”xxx“）)
            if (removeByIds(ids)) {
                return Result.success("删除成功");
            }
            return Result.error("删除失败","500");
        }catch (Exception e){
            return Result.error("批量删除发生异常"+String.valueOf(e),"500");
        }
    }
}




